package com.qf.controller;

import com.qf.entity.Dept;
import com.qf.entity.Emp;
import com.qf.entity.PageInfo;
import com.qf.service.DeptService;
import com.qf.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.sql.SQLException;
import java.util.List;

/**
 * User: key
 * ClassName:EmpController
 * Date: 2023/5/25
 * Time: 16:36
 * description:
 */
@Controller
@RequestMapping("/emp")
public class EmpController {

    @Autowired
    private EmpService empService;
    @Autowired
    private DeptService deptService;

    @RequestMapping("/list")
    public String empList(Model model) throws SQLException {
        List<Emp> emps = empService.selectAll();
        model.addAttribute("empList", emps);
        return "emp/empList";
    }

    @RequestMapping(value = "/deptMsg")
    public String deptMsg(Model model) {
        //这个步骤是将部门信息从数据库取出来
        List<Dept> depts = deptService.selectDept();
//        for (Dept dept : depts) {
//            System.out.println(dept);
//        }
        model.addAttribute("deptList", depts);
        return "emp/addEmp";
    }

    @RequestMapping(value = "/addEmp", method = RequestMethod.POST)
    public String addEmp(Emp emp) throws SQLException {
        //这个步骤是将部门信息从数据库取出来
//        List<Dept> depts = deptService.selectDept();
//        model.addAttribute("deptList",depts);


        Integer integer = empService.insert(emp);
//        System.out.println(integer);
        return "redirect:list";
    }

    @RequestMapping(value = "/deleteEmp", method = RequestMethod.GET)
    public String deleteEmp(@RequestParam(value = "eno", required = false, defaultValue = "1") Integer eno/*,Model model*/) throws SQLException {
        empService.delete(eno);
//        List<Emp> emps = empService.selectAll();
//        model.addAttribute("empList", emps);
//        return "emp/empList";
        return "redirect:list";
    }

    @RequestMapping(value = "/updateEmp")
    public String updateEmp(@RequestParam(value = "eno", required = false, defaultValue = "1") Integer eno, Model model) throws SQLException {
        Emp emp = empService.select(eno);
        System.out.println(eno);
        //这一步为止是是将值渲染到页面
        model.addAttribute("oneEmp", emp);
        List<Dept> depts = deptService.selectDept();
        model.addAttribute("deptList", depts);

//        Integer update = empService.update(emp);
//        System.out.println(update);
//        return "redirect:list";
//        return "forward:list";
        return "emp/updateEmp";
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(Emp emp) throws SQLException {

        System.out.println(emp.getEno() + "-----");
        Integer update = empService.update(emp);
        System.out.println(update);
        return "redirect:list";
    }
//    @RequestMapping("/findAll03")
//    public ModelAndView findAllByPage(@RequestParam(defaultValue = "1") int currentPage) {
//        ModelAndView mv = new ModelAndView();
//        //获取分页信息
//        PageInfo<Emp> pageInfo = empService.findByPage(currentPage);
//        System.out.println(pageInfo);
//        // 将数据返回到页面上
//        mv.setViewName("pages/dataList");
//        mv.addObject("pageInfo", pageInfo);
//        return mv;
//    }
}
